package com.jawmask.android.discover.ui;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;

import com.jawmask.android.discover.R;
import com.jawmask.android.discover.utils.AppConstants;

public class BaseListActivity extends ListActivity {

	private String errorMessage;
	private String notificationMessage;

	public void waitForData(final String notificationMessage) {
		this.notificationMessage = notificationMessage;
		showDialog(AppConstants.DIALOG_ID_PROGRESS_BAR);
	}

	public void receivedData() {
		dismissDialog(AppConstants.DIALOG_ID_PROGRESS_BAR);
	}

	public void showNotification(final String errorMessage) {
		this.errorMessage = errorMessage;

		showDialog(AppConstants.DIALOG_ID_ERROR_NOTIFICATION);
	}

	@Override
	protected Dialog onCreateDialog(int dialogId) {
		Dialog dialog = null;

		switch (dialogId) {
		case AppConstants.DIALOG_ID_PROGRESS_BAR:
			ProgressDialog progressDialog = new ProgressDialog(this);
			progressDialog.setMessage(this.notificationMessage);
			dialog = progressDialog;
			break;
		case AppConstants.DIALOG_ID_NETWORK_NOT_AVAILABLE:
			Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(getString(R.string.error_msg_network_not_available));
			builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					System.exit(0);
				}
			});
			dialog = builder.create();
			break;
		case AppConstants.DIALOG_ID_UNKNOWN_ERROR:
			builder = new AlertDialog.Builder(this);
			builder.setMessage(getString(R.string.error_msg_unknow_error));
			builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					System.exit(0);
				}
			});
			dialog = builder.create();
			break;
		case AppConstants.DIALOG_ID_ERROR_NOTIFICATION:
			builder = new AlertDialog.Builder(this);
			builder.setTitle(getString(R.string.dlg_notification_title_txt));
			builder.setMessage(errorMessage);
			builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					removeDialog(AppConstants.DIALOG_ID_ERROR_NOTIFICATION);
				}
			});
			builder.setCancelable(false);
			dialog = builder.create();
			break;
		}
		return dialog;
	}
}